package team.umbc;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class CheckoutActivity extends Activity implements OnClickListener {
	
	private PlacesDB placesDB;
	private SQLiteDatabase db;
	private Cursor cursor;
	private SharedPreferences settings;
	private String place;
	private Facebook facebook;
	private boolean fbFlag, fsFlag;
	
	TextView placeTitle;
	EditText message;
	Button tagBtn, checkBtn, backBtn;
	ToggleButton fbBtn, fsBtn;
	
	
	@Override
	public void onCreate(Bundle savedInstance){
		super.onCreate(savedInstance);
		setContentView(R.layout.checkout);
		
		settings = getSharedPreferences(CheckinActivity.PREFS_NAME, 0);
		place = settings.getString("checkin", "place");
		facebook = new Facebook(CheckinActivity.APP_ID);
		facebook.setAccessToken(settings.getString("AccessToken", ""));
		
		
		placesDB = new PlacesDB(this);
		db = placesDB.getReadableDatabase();
		cursor = db.query(PlacesDB.TABLE, null, PlacesDB.P_NAME + " = '" + place + "'", null, null, null, null);
		startManagingCursor(cursor);
		cursor.moveToFirst();
		
		//Bundle services = getIntent().getExtras();
		//facebook = (Facebook)services.getSerializable("facebook");   
		
		placeTitle = (TextView)findViewById(R.id.place_title);
		placeTitle.setText(place);
		message = (EditText)findViewById(R.id.message_text);
		
		tagBtn = (Button)findViewById(R.id.tag_button);
		tagBtn.setOnClickListener(this);
		
		fbBtn = (ToggleButton)findViewById(R.id.fb_button);
		fbBtn.setOnClickListener(this);
		
		fsBtn = (ToggleButton)findViewById(R.id.fs_button);
		fsBtn.setOnClickListener(this);
		
		checkBtn = (Button)findViewById(R.id.checkout_button);
		checkBtn.setOnClickListener(this);
		
		backBtn = (Button)findViewById(R.id.back_button);
		backBtn.setOnClickListener(this);
		
		message = (EditText)findViewById(R.id.message_text);
		System.out.println(settings.getString("fbID", ""));
		
	}
	 @Override
	public void onResume(){
		super.onResume();
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
		db.close();
		cursor.close();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tag_button:
			startActivity(new Intent(this, FriendActivity.class));
			break;
		case R.id.fb_button:
			fbFlag = !fbFlag;
			break;
		case R.id.fs_button:
			System.out.println(message.getText().toString());
			fsFlag = !fsFlag;
			break;
		case R.id.checkout_button:
			checkout();
			break;
		case R.id.back_button:
			finish();
			break;
		}
	}

	private void checkout(){
		if(facebook.isSessionValid()){
			settings = getSharedPreferences(CheckinActivity.PREFS_NAME, 0);
			
			Bundle params = new Bundle();			
			params.putString("access_token", facebook.getAccessToken());
			params.putString("place", settings.getString("fbID", ""));
			params.putString("message", message.getText().toString());
			//params.putString("tags", settings.getString("friend", ""));
			JSONObject coordinates = new JSONObject();
			try {
				String latCoor = settings.getString("lat", "");
				String longCoor = settings.getString("long", "");
				
				coordinates.put("latitude", latCoor);
				coordinates.put("longitude", longCoor);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			params.putString("coordinates",coordinates.toString());
			
			String ids = settings.getString("friend", "");
			String temp = "";
			/*JSONArray frnd_data=new JSONArray();
			for(int i = 0; i < ids.length(); i++){
				if(ids.charAt(i) != ','){
					temp += ids.charAt(i);
				} else {
					frnd_data.put(temp);
					temp = "";
				}
			}*/
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>" + ids);
			params.putString("tags", ids);
			try {
				String response = facebook.request("me/checkins", params,"POST");
				System.out.println(response);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			facebook.authorize(this, new String[] {"publish_checkins", "read_friendlists", "offline_access"} , new DialogListener(){

				@Override
				public void onComplete(Bundle values) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onFacebookError(FacebookError e) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onError(DialogError e) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onCancel() {
					// TODO Auto-generated method stub
					
				}});
		}
	}

}
